<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>免费会员等级管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>

            <h3 style="text-align: left;border-bottom:1px solid gainsboro;padding-bottom: 5px">免费会员等级查询</h3>
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入等级名称" clearable v-model="memberName" @clear="searchMember">
                        <el-button slot="append" icon="el-icon-search" @click="searchMember"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary"  @click="readyAdd">添加会员等级</el-button>
                </el-col>
            </el-row>
        </el-card>
      <el-card style="margin-top: 30px">
            <el-table :data="memberList" border stripe>
                <el-table-column type="index"></el-table-column>
              <el-table-column label="等级" prop="level"></el-table-column>
              <el-table-column label="等级名称" prop="membername"></el-table-column>
              <el-table-column label="成长值">
                <template slot-scope="scope">{{scope.row.growthvaluemin}}—{{scope.row.growthvaluemax}}</template>
              </el-table-column>
              <el-table-column label="升级礼包" prop="gift.giftname"></el-table-column>
              <el-table-column label="会员权益" >
                <template slot-scope="sc">
                  <el-button type="text" size="mini" @click="showEquity(sc.row)">查看</el-button>
                  <el-button type="text" size="mini" @click="editEquity(sc.row)">编辑</el-button>
                </template>
              </el-table-column>
              <el-table-column label="等级状态">
                  <template slot-scope="sc">{{sc.row.status==1?'启用':'禁用'}}</template>
              </el-table-column>
              <el-table-column label="最后修改时间" prop="updatetime"></el-table-column>
                <el-table-column label="操作" width="250px">
                    <template slot-scope="scope">
                        <el-button type="text"  @click="showEditDialog(scope.row)">编辑</el-button>
                        <el-button type="text"  @click="removeById(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[5, 10 , 15, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
            ></el-pagination>
        </el-card>

      <el-dialog title="会员拥有权益" :visible.sync="showEquityVisible"  width="30%" :close-on-click-modal="false" @close="equityFormClose">
        <p style="font-size: 16px;border-bottom: 1px solid gainsboro;text-align: left">会员信息</p>
        <p style="text-align: left">会员等级名称：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span v-text="vname"></span></p>
        <p style="font-size: 14px;border-bottom: 1px solid gainsboro;text-align: left">拥有权益</p>
        <p style="text-align: left" v-for="n of vipEquityList" >{{n.name}}</p>
        <span slot="footer" class="dialog-footer">
                <el-button @click="equityFormClose">关 闭</el-button>
            </span>
      </el-dialog>

      <el-dialog title="编辑会员拥有权益" :visible.sync="editEquityVisible"  width="30%" :close-on-click-modal="false" @close="editEquityFormClose">
        <p style="font-size: 16px;border-bottom: 1px solid gainsboro;text-align: left">会员信息</p>
        <p style="text-align: left">会员等级名称：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span v-text="vname"></span></p>
        <p style="font-size: 16px;border-bottom: 1px solid gainsboro;text-align: left">条件设置</p>
        <el-table
            ref="multipleTable"
            :data="allEquityList"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange">
          <el-table-column
              type="selection"
              width="55">
          </el-table-column>
          <el-table-column
              prop="id"
              label="id"
              width="50px">
          </el-table-column>
          <el-table-column
              prop="name"
              label="权益名称"
              width="120">
          </el-table-column>
        </el-table>
        <span slot="footer" class="dialog-footer">
                <el-button @click="doEditEquity">确 定</el-button>
                <el-button @click="editEquityFormClose">关 闭</el-button>
            </span>
      </el-dialog>


      <!--   添加会员等级   -->
      <el-dialog title="添加会员等级" :visible.sync="addFormVisible" width="50%" :close-on-click-modal="false" @close="addFormClose">
        <el-form :model="addForm" :rules="addRules" ref="addForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="会员等级" prop="level">
            <el-input v-model="addForm.level"></el-input>
          </el-form-item>
          <el-form-item label="等级名称" prop="membername">
            <el-input v-model="addForm.membername"></el-input>
          </el-form-item>
          <el-form-item label="成长值下限"prop="growthvaluemin">
            <el-input v-model="addForm.growthvaluemin"></el-input>
          </el-form-item>
          <el-form-item label="成长值上限"prop="growthvaluemax">
            <el-input v-model="addForm.growthvaluemax"></el-input>
          </el-form-item>
          <el-form-item label="升级礼包" prop="giftbagid">
            <el-select v-model="addForm.giftbagid"  placeholder="请选择礼包">
              <el-option :label="item.giftname" :value="item.id" v-for="item in giftList" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="doAdd">确 定</el-button>
                <el-button @click="addFormClose">取 消</el-button>
            </span>
      </el-dialog>

      <!--   修改会员等级   -->
      <el-dialog title="修改会员等级" :visible.sync="editFormVisible" width="50%" :close-on-click-modal="false" @close="editFormClose">
        <el-form :model="editForm" :rules="editRules" ref="editForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="会员等级" prop="level">
            <el-input v-model="editForm.level"></el-input>
          </el-form-item>
          <el-form-item label="等级名称" prop="membername">
            <el-input v-model="editForm.membername"></el-input>
          </el-form-item>
          <el-form-item label="成长值下限"prop="growthvaluemin">
            <el-input v-model="editForm.growthvaluemin"></el-input>
          </el-form-item>
          <el-form-item label="成长值上限"prop="growthvaluemax">
            <el-input v-model="editForm.growthvaluemax"></el-input>
          </el-form-item>
          <el-form-item label="标签类型"  prop="status">
            <el-radio v-model="editForm.status" :label="1">启用</el-radio>
            <el-radio v-model="editForm.status" :label="0">禁用</el-radio>
          </el-form-item>
          <el-form-item label="升级礼包" prop="giftbagid">
            <el-select v-model="editForm.giftbagid"  placeholder="请选择礼包">
              <el-option :label="item.giftname" :value="item.id" v-for="item in giftList" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="doEdit">确 定</el-button>
                <el-button @click="editFormClose">取 消</el-button>
            </span>
      </el-dialog>

    </div>
</template>
<script>
    export default {
        data() {
            return {
                pageNum: 1,
                pageSize: 5,
                memberList: [],
                giftList:[],//所有礼物列表
                total: 9,
                memberName: "",
                vname:"",
                vid:"",
                addFormVisible: false,
                editFormVisible:false,
                showEquityVisible:false,
                editEquityVisible:false,
                vipEquityList:[],
                allEquityList:[],
                multipleSelection:[],
                addForm: {
                  id:"",
                  level:"",
                  membername:"",
                  growthvaluemin:"",
                  growthvaluemax:"",
                  giftbagid:"",
                },
                editForm: {
                id:"",
                level:"",
                membername:"",
                growthvaluemin:"",
                growthvaluemax:"",
                giftbagid:"",
                  status:""
              },
              addRules: {
                   /*  userCode: [
                       { required: true, message: "請輸入工号", trigger: "blur" },
                       { min: 4, max: 4, message: "工号必须是4个字符", trigger: "blur" }
                     ]*/
                },
              editRules:{

              },
              editEquityFrom:{
                  id:"",
                equityIds:[]
              }
            };
        },
        methods: {
          doEditEquity(){
            console.log(this.multipleSelection)
            let equitys=[]
            for (let i=0;i<this.multipleSelection.length;i++){
              equitys.push(this.multipleSelection[i].id)
            }
            this.editEquityFrom.id=this.vid;
            this.editEquityFrom.equityIds=equitys;

            let url = "member/updateMemberEquity";
            this.$axios.put(url,this.editEquityFrom).then(res=>{
              if (res.data.code == 200){
                this.$message({
                  type:"success",
                  message:res.data.msg,
                  duration:1000
                })
                this.editEquityVisible=false;
                this.vipEquityList=[];
                this.searchMember();
              }else {
                this.$message({
                  type:"error",
                  message:res.data.msg,
                  duration:1000
                })
              }
            });

          },
          handleSelectionChange(val){
            this.multipleSelection = val;
          },
          editEquity(row){
            this.editEquityVisible=true;
            this.vname=row.name;
            this.vid=row.id;
            //查询所有的权益
            let url = "member/queryAllEquity";
            this.$axios.get(url).then(res=>{
              if (res.data.code == 200){
                this.allEquityList = res.data.data;
              }else {
                this.$message({
                  type:"error",
                  message:res.data.msg,
                  duration:1000
                })
              }
            });
            //查询该等级拥有的权限
            let id=row.id;
            let murl = "member/queryEquityByMemberId";
            this.$axios.get(murl,{
              params:{
                mid:id,
              }
            }).then(res=>{
              if (res.data.code == 200){
                this.vipEquityList = res.data.data;
                this.allEquityList.forEach((grt)=>{
                  this.vipEquityList.forEach((con) =>{
                    if(grt.id==con.id){
                      this.$refs.multipleTable.toggleRowSelection(grt,true);
                    }
                  })
                })
              }else {
                this.$message({
                  type:"error",
                  message:res.data.msg,
                  duration:1000
                })
              }
            });

          },
          editEquityFormClose(){
            this.editEquityVisible=false;
            this.vname="";
          },
          equityFormClose(){
            this.showEquityVisible=false;
            this.vname="";
            this.vipEquityList=[];
          },
          showEquity(row){
            this.vname=row.membername
            this.showEquityVisible=true
            //查询该等级拥有的权益
            let id=row.id;
            let url = "member/queryEquityByMemberId";
            this.$axios.get(url,{
              params:{
                mid:id,
              }
            }).then(res=>{
              if (res.data.code == 200){
                this.vipEquityList = res.data.data;
              }else {
                this.$message({
                  type:"error",
                  message:res.data.msg,
                  duration:1000
                })
              }
            });
          },
          doEdit(){

            this.$refs.editForm.validate(valid=>{
              if (!valid){
                return ;
              }
              let url = "member/updateMember";
              this.$axios.put(url,this.editForm).then(res=>{
                if (res.data.code == 200){
                  this.$message({
                    type:"success",
                    message:res.data.msg,
                    duration:1000
                  })
                  this.editFormVisible = false ;
                  this.$refs.editForm.resetFields();
                  this.searchMember();
                }else {
                  this.$message({
                    type:"error",
                    message:res.data.msg,
                    duration:1000
                  })
                }
              });
            })

          },
          editFormClose(){
            this.editFormVisible=false
            this.$refs.editForm.resetFields();
          },
          showEditDialog(row) {
            this.editFormVisible=true;
            this.editForm=row;
          },
            removeById(id) {
              this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                    let url = "member/del/"+id ;
                    this.$axios.delete(url)
                        .then(res=>{
                          if (res.data.code == 200){
                            this.$message({
                              type:"success",
                              message:res.data.msg,
                              duration:1000
                            })
                            this.searchMember();
                          }else {
                            this.$message({
                              type:"error",
                              message:res.data.msg,
                              duration:1000
                            })
                          }
                        })
                  }).catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消删除'
                });
              });

            },
          readyAdd(){
            this.addFormVisible=true;
          },
          doAdd() {
              this.$refs.addForm.validate(valid=>{
                if (!valid){
                  return ;
                }
                console.log(this.addForm)

                let url = "member/add";
                this.$axios.post(url,this.addForm).then(res=>{
                  if (res.data.code == 200){
                    this.$message({
                      type:"success",
                      message:res.data.msg,
                      duration:1000
                    })
                    this.addFormVisible = false ;
                    this.$refs.addForm.resetFields();
                    this.searchMember();
                  }else {
                    this.$message({
                      type:"error",
                      message:res.data.msg,
                      duration:1000
                    })
                  }
                });


              })
            },
          addFormClose() {
                this.$refs.addForm.resetFields();
                this.addFormVisible=false;
            },
          getGiftList(){
            let url = "member/queryAllGift";
            this.$axios.get(url).then(res=>{
              if (res.data.code == 200){
                this.giftList = res.data.data;
              }else {
                this.$message({
                  type:"error",
                  message:res.data.msg,
                  duration:1000
                })
              }
            });

          },
          getMemberList() {
              let url = "member/queryAllMemberByPage";
              this.$axios.get(url,{
                params:{
                  name:this.memberName,
                  now:this.pageNum,
                  size:this.pageSize
                }
              }).then(res=>{
                if (res.data.code == 200){
                  this.memberList = res.data.data.list;
                  this.pageNum = res.data.data.pageNum;
                  this.pageSize = res.data.data.pageSize;
                  this.total = res.data.data.total;
                }else {
                  this.$message({
                    type:"error",
                    message:res.data.msg,
                    duration:1000
                  })
                }
              });
            },
            searchMember() {
                this.pageNum = 1;
                this.getMemberList();
            },
            handleSizeChange(newSize) {
                this.pageSize = newSize;
                this.getMemberList();
            },
            handleCurrentChange(newPage) {
                this.pageNum = newPage;
                this.getMemberList();
            },
            reloadData() {
                this.searchMember();
                this.getMemberList();
                this.getGiftList();
            }
        },
        created() {
            this.reloadData();

        }
    };
</script>
<style lang="less" scoped>
</style>